/* eslint-disable */
/**
 * name: 全局过滤器（转化数据方法）
 * author: add
 * time: 2018/9/4
 */
export default {
    /**
     * 根据ID获取设备名称
     * @param that this
     * @param id 设备ID （需要转换为int类型）
     */
    getEquipmentName(that, id) {
        for (let j = 0; j < that.$store.state.configData.length; j++) {
            if (parseInt(that.$store.state.configData[j].id) === id) {
                return that.$store.state.configData[j].name;
            }
        }
    },
    /**
     * 根据名称获取设备ID
     * @param that this
     * @param name 设备名称
     */
    getEquipmentId(that, name) {
        for (let j = 0; j < that.$store.state.configData.length; j++) {
            if (that.$store.state.configData[j].name === name) {
                return that.$store.state.configData[j].id;
            }
        }
    },
    /**
     * 转义时间戳方法合集
     */
    timesFormat(stringTime) {
        return (new Date(stringTime)).getTime();
    },
    // eg:2018-07-01 转化到190几年到天
    formatDate(inputTime) {
        try {
            inputTime = parseInt(inputTime);
        } catch (e) {
            inputTime = 0;
        }
        let date = new Date(inputTime);
        let y = date.getFullYear();
        let m = date.getMonth() + 1;
        m = m < 10 ? ("0" + m) : m;
        let d = date.getDate();
        d = d < 10 ? ("0" + d) : d;
        return y + "-" + m + "-" + d;
    },
    // 2018-07-01 转化有月日汉字
    formatDateZH(inputTime) {
        try {
            inputTime = parseInt(inputTime);
        } catch (e) {
            inputTime = 0;
        }
        let date = new Date(inputTime);
        let m = date.getMonth() + 1;
        // m = m < 10 ? ('0' + m) : m;
        let d = date.getDate();
        // d = d < 10 ? ('0' + d) : d;
        return m + "月" + d + "日";
    },
    // 时间戳转时间格式（用于组件）
    formatDateTime(inputTime) {
        let date = new Date(inputTime);
        let y = date.getFullYear();
        let m = date.getMonth() + 1;
        m = m < 10 ? ("0" + m) : m;
        let d = date.getDate();
        d = d < 10 ? ("0" + d) : d;
        let h = date.getHours();
        h = h < 10 ? ("0" + h) : h;
        let minute = date.getMinutes();
        let second = date.getSeconds();
        minute = minute < 10 ? ("0" + minute) : minute;
        second = second < 10 ? ("0" + second) : second;
        return y + "-" + m + "-" + d + "   " + h + ":" + minute + ":" + second;
    },
    // eg:2018-07-01 转化到190几年
    formatDateTimeInput(inputTime) {
        try {
            inputTime = parseInt(inputTime);
        } catch (e) {
            inputTime = 0;
        }
        let date = new Date(inputTime);
        let y = date.getFullYear();
        let m = date.getMonth() + 1;
        m = m < 10 ? ("0" + m) : m;
        let d = date.getDate();
        d = d < 10 ? ("0" + d) : d;
        let h = date.getHours();
        h = h < 10 ? ("0" + h) : h;
        let minute = date.getMinutes();
        minute = minute < 10 ? ("0" + minute) : minute;
        return y + "-" + m + "-" + d + " " + h + ":" + minute;
    },
    // 转化[年月日, 时分秒]
    formatDateTimeArray(inputTime) {
        try {
            inputTime = parseInt(inputTime);
        } catch (e) {
            inputTime = 0;
        }
        let param = [];
        let date = new Date(inputTime);
        let y = date.getFullYear();
        let m = date.getMonth() + 1;
        m = m < 10 ? ("0" + m) : m;
        let d = date.getDate();
        d = d < 10 ? ("0" + d) : d;
        let h = date.getHours();
        h = h < 10 ? ("0" + h) : h;
        let minute = date.getMinutes();
        let second = date.getSeconds();
        minute = minute < 10 ? ("0" + minute) : minute;
        second = second < 10 ? ("0" + second) : second;
        param[0] = y + "-" + m + "-" + d;
        param[1] = h + ":" + minute + ":" + second;
        return param;
    },
    // 返回周岁年龄
    age(inputTime) {
        let date = new Date(inputTime);
        let y = date.getFullYear();
        let m = date.getMonth() + 1;
        m = m < 10 ? ("0" + m) : m;
        let d = date.getDate();
        d = d < 10 ? ("0" + d) : d;
        let h = date.getHours();
        h = h < 10 ? ("0" + h) : h;
        let minute = date.getMinutes();
        let second = date.getSeconds();
        minute = minute < 10 ? ("0" + minute) : minute;
        second = second < 10 ? ("0" + second) : second;
        let BirthDate = y + "-" + m + "-" + d + "  " + h + "-" + minute + "-" + second;
        let returnAge;
        let strBirthdayArr = BirthDate.split("-");
        let birthYear = strBirthdayArr[0];
        let birthMonth = strBirthdayArr[1];
        let birthDay = strBirthdayArr[2];
        d = new Date();
        let nowYear = d.getFullYear();
        let nowMonth = d.getMonth() + 1;
        let nowDay = d.getDate();
        if (nowYear === birthYear) {
            returnAge = 0;// 同年 则为0岁
        } else {
            let ageDiff = nowYear - birthYear; // 年之差
            if (ageDiff > 0) {
                if (nowMonth === birthMonth) {
                    let dayDiff = nowDay - birthDay;// 日之差
                    if (dayDiff < 0) {
                        returnAge = ageDiff - 1;
                    } else {
                        returnAge = ageDiff;
                    }
                } else {
                    let monthDiff = nowMonth - birthMonth;// 月之差
                    if (monthDiff < 0) {
                        returnAge = ageDiff - 1;
                    } else {
                        returnAge = ageDiff;
                    }
                }
            } else {
                returnAge = -1;// 返回-1 表示出生日期输入错误 晚于今天
            }
        }
        return returnAge;
    },
    // 公用拼凑表格组件搜索参数
    SearchEvent(searchName, page, perPage) {
        let obj = {};
        searchName.map(t => {
            obj[t.key] = "";
        });
        return {...obj, ...{page: page, per_page: perPage}};
    },
    // 转换string类型
    stringEvent(data) {
        let dataKey = Number(data) ? (data).toString() : data;// string
        return dataKey;
    },
    // 转换int类型
    intEvent(data) {
        let dataKey = typeof(data) === "string" ? parseInt(data) : data;// int
        return dataKey;
    }
};
